#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "BoneList.h"
#include "Bone.h"
#include "GL/glut.h"
BoneList::BoneList()
{
	head = NULL;
}
BoneList::BoneList(Bone * headBone)
{
	head = new List<Bone>(headBone, NULL);
}
BoneList::~BoneList()
{
	printf("deleting bone list\n");
	List<Bone> * search = head;
	while(search)
	{
		search = search->RemoveList();
	}
	head = NULL;
	printf("done deleting bones\n");
}
List<Bone> * BoneList::Add(Bone * payload)
{
	head = new List<Bone>(payload, head);
	return head;
}
void BoneList::Remove(Bone * point)
{		
	List<Bone> * search = head;
	while(search)
	{
		if(search->payload && search->payload ==  point)
		{
			if(search == head)
			{
				head = search->next;
			}
			search = search->Remove();
			break;
		}
		else
			search = search->next;
	}	
}

Bone * BoneList::GetHoverBone(Vector2D headPosition, Mouse * mouse)
{
	List<Bone> * search = head;
	while(search)
	{
		if(search->payload)
		{
			if(((search->payload->GetEndPoint().GetPosition() + headPosition) | mouse->GetPosition()) < 0.02)
				return search->payload;
		}
		search = search->next;
	}	
	return NULL;
}
char * BoneList::GetPose()
{
	List<Bone> * search = head;
	
	int length = 0;
	char * tag = NULL;
	
	while(search)
	{
		if(search->payload)
		{
			char * point = search->payload->GetPose();
			int commaSpace = 0;
			if(search->next)
				commaSpace = 1;
			tag = (char *)realloc(tag, (length + strlen(point) + 1 + commaSpace) * sizeof(char));
			memcpy(tag+length,point, strlen(point)+1);
	
			if(commaSpace)
			{
				int len = strlen(tag);
				tag[len] = ',';
				tag[len+1]='\0';
			}
			length += strlen(point)+commaSpace;
			delete point;
		}
		search = search->next;
	}
	return tag;
}
char * BoneList::GetTag()
{
	List<Bone> * search = head;
	
	int length = 0;
	char * tag = NULL;
	
	while(search)
	{
		if(search->payload)
		{
			char * point = search->payload->GetTag();
			int commaSpace = 0;
			if(search->next)
				commaSpace = 1;
			tag = (char *)realloc(tag, (length + strlen(point) + 1 + commaSpace) * sizeof(char));
			memcpy(tag+length,point, strlen(point)+1);
	
			if(commaSpace)
			{
				int len = strlen(tag);
				tag[len] = ',';
				tag[len+1]='\0';
			}
			length += strlen(point)+commaSpace;
			delete point;
		}
		search = search->next;
	}
	return tag;
}
void BoneList::Draw()
{
	List<Bone> * search = head;
	
	while(search)
	{
		if(search->payload)
		{
			search->payload->Draw();
		}
		search = search->next;
	}
}
void BoneList::Drag(Vector2D headPosition, Mouse* mouse)
{
	printf("BoneList::Drag\n");
	List<Bone> * search = head;
	
	while(search)
	{
		if(search->payload)
		{
			search->payload->Drag(headPosition, mouse);
		}
		search = search->next;
	}
}
Bone * BoneList::Click(Vector2D headPosition, Mouse * mouse)
{
	List<Bone> * search = head;
	
	while(search)
	{
		if(search->payload)
		{
			Bone * result = NULL;
			if((result = search->payload->Click(headPosition, mouse)))
				return result;
		}
		search = search->next;
	}
	return NULL;
}
int BoneList::GetLength()
{
	List<Bone> * search = head;
	int i =0;
	while(search)
	{
		i++;
		search = search->next;
	}
	return i;
}
void BoneList::CalculateAngleLength()
{
	List<Bone> * search = head;
	
	while(search)
	{
		if(search->payload)
		{
			search->payload->CalculateAngleLength();
		}
		search = search->next;
	}
}
void BoneList::CalculatePosition()
{
	List<Bone> * search = head;
	
	while(search)
	{
		if(search->payload)
		{
			search->payload->CalculatePosition();
		}
		search = search->next;
	}
}
void BoneList::RunFunction( void (Bone::*func)(void))
{
	List<Bone> * search = head;
	
	while(search)
	{
		if(search->payload)
		{
			(search->payload->*func)();
		}
		search = search->next;
	}
}
